*** This test treatment effects of "training, no grant" vs "training and grant"

if ("`swdLocal'\Data"=="") {
	di as error "Please run init.do first."
	error 1
}
clear all
set more off
set maxvar 10000

*Open file for regressions
use "${gsdAnalysisOutput}/bscie_panel_indices_final", clear

*Keep only endline and lagged controls
keep if wave==2 & final_sample==1

*Define strata
egen strata=group(gender state_old)

gen treatmentX=0
replace treatmentX=1 if treatment_stream==3

ren edu_level_b  edu_b

*Generate IV treatment x kcb_dist 
gen Z_treat_kcb=treatment*lg_kcb_dist
label var Z_treat_kcb "Treatment x (log) distance to KCB branch"

*Generate interaction for other geographic controls
gen treat_citydist=treatment*lg_city_dist
label var treat_citydist "Treatment x (log) distance to city center"
gen treat_roaddist=treatment*lg_road_dist
label var treat_roaddist "Treatment x (log) distance to primary road"
gen treat_gradient=treatment*gradient
label var treat_gradient "Treatment x gradient"
gen treat_conflict = treatment*death_x_prox_a300
label var treat_conflict "Treatment x conflict_affected_index(300km buffer)"
gen treat_edu = treatment*edu_b
gen treat_num = treatment*num_eval_b
gen treat_lit = treatment*lit_eval_b

* Generate sum of interaction and level
gen TplusD = treatment + lg_kcb_dist 
gen TtimesDminus1 = treatment * (lg_kcb_dist - 1)
gen TplusNogrant = treatment + treatmentX
gen TtimesNOgrantminus1 = treatment * (treatmentX - 1)

*Define geographic controls
global geo_controls lg_city_dist treat_citydist treat_roaddist lg_road_dist gradient treat_gradient death_x_prox_a300 treat_conflict

global base_controls i.edu_b i.treat_edu i.num_eval_b i.treat_num i.lit_eval_b i.treat_lit

*** Define variables that we need to estimate the join significance of lg_kcb_dist + treatmentX

gen blub = treatmentX - treatment
gen blubIV = c.lg_kcb_dist#1.treatment - treatment

*** Regressions ****************************************************************


mat A = J(5,12,.)
mat N = J(1,6,.)
mat D = J(1,6,.)
mat stars = J(5,12,0)

global n 1

foreach var of varlist food_calc food_spend_total food_grow_total nonfood_spend_total asset_spend_total  {

	eststo: xi: ivreg2 `var' lg_kcb_dist treatment $geo_controls  i.strata (treatmentX= Z_treat_kcb) , cluster(boma_old) first savefirst savefprefix(fstage_) 
local b_t1 = _b[treatmentX]
		display `b_t1'
		mat A[$n, 1] = `b_t1'
		local p_t1=(2 * ttail(e(ardf_r), abs(_b[treatmentX]/_se[treatmentX])))
		display `p_t1'
		mat A[$n, 2] = `p_t1'
		local b_t2 = _b[treatment]
		display `b_t2'
		mat A[$n, 3] = `b_t2'
		local p_t2=(2 * ttail(e(ardf_r), abs(_b[treatment]/_se[treatment])))
		display `p_t2'
		mat A[$n, 4] = `p_t2'
		local obs = `e(N)'
		mat N[1,3] = `obs'
		local fstat = `e(rkf)'
		mat D[1,3] = `fstat'
		
		lincom treatmentX + treatment
		local b_t1 = `r(estimate)'
		display `b_t1'
		mat A[$n,5] = `b_t1'
		local p_t1= `r(p)'
		display `p_t1'
		mat A[$n,6] = `p_t1'
		
				matrix stars[$n, 1] = (A[$n, 2] < 0.10) + (A[$n, 2] < 0.05) + (A[$n, 2] < 0.01)	
				matrix stars[$n, 3] = (A[$n, 4] < 0.10) + (A[$n, 4] < 0.05) + (A[$n, 4] < 0.01)	
				matrix stars[$n, 5] = (A[$n, 6] < 0.10) + (A[$n, 6] < 0.05) + (A[$n, 6] < 0.01)	

eststo: xi: ivreg2 `var' lg_kcb_dist treatment $geo_controls $base_controls  i.strata (treatmentX= Z_treat_kcb) , cluster(boma_old) first savefirst savefprefix(fstage_) 
local b_t1 = _b[treatmentX]
		display `b_t1'
		mat A[$n, 7] = `b_t1'
		local p_t1=(2 * ttail(e(ardf_r), abs(_b[treatmentX]/_se[treatmentX])))
		display `p_t1'
		mat A[$n, 8] = `p_t1'
		local b_t2 = _b[treatment]
		display `b_t2'
		mat A[$n, 9] = `b_t2'
		local p_t2=(2 * ttail(e(ardf_r), abs(_b[treatment]/_se[treatment])))
		display `p_t2'
		mat A[$n, 10] = `p_t2'
		local obs = `e(N)'
		mat N[1,6] = `obs'
		local fstat = `e(rkf)'
		mat D[1,6] = `fstat'
		
		lincom treatmentX + treatment
		local b_t1 = `r(estimate)'
		display `b_t1'
		mat A[$n,11] = `b_t1'
		local p_t1= `r(p)'
		display `p_t1'
		mat A[$n,12] = `p_t1'
		
				matrix stars[$n, 7] = (A[$n, 8] < 0.10) + (A[$n, 8] < 0.05) + (A[$n, 8] < 0.01)	
				matrix stars[$n, 9] = (A[$n, 10] < 0.10) + (A[$n, 10] < 0.05) + (A[$n, 10] < 0.01)	
				matrix stars[$n, 11] = (A[$n, 12] < 0.10) + (A[$n, 12] < 0.05) + (A[$n, 12] < 0.01)	

		
		global n = $n + 1
		
}

		matrix rownames A = "\# food items" "(log) food purchased" "(log) food home production" "(log) non-food consumption" "(log) asset acquision" 
		frmttable , statmat(A) replace sdec(3)  substat(1) plain annotate(stars) asymbol(*,**,***)  /// 
		title (Table 5A: Consumption index components) ///
		ctitles("") ///
		note("Note: ")
		frmttable , statmat(N) replace sdec(0) append rtitles("Observations")
		frmttable using "$gsdTables/Table5A.tex", statmat(D) replay replace tex sdec(3) append rtitles("F-stat") plain